package com.jboost.modules.app.service.strategry.xmallure;

import com.google.common.collect.Maps;
import com.jboost.common.util.EncryptSha256Util;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.Strategy;
import com.jboost.modules.app.dto.XmFieldDto;
import com.jboost.modules.system.entity.AJdySyncData;
import com.jboost.modules.system.entity.SysUserDepart;
import com.jboost.modules.system.mapper.GcDataMapper;
import com.jboost.modules.system.mapper.SysUserDepartMapper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.jboost.modules.app.service.strategry.xm.XmUserQuery.getParams;

@Service
@Slf4j
public abstract class AbstractXmAllureUser<T, V> implements Strategy<T,V> {




    protected HttpHeaders jdyHeaders;
    {
        jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "c7JYwlozr6024iavsQ0i0iZ2078zog2Z");

    }


    public void run(ApplicationArguments args) throws Exception {
      invokeStrategy();
    }

   public HttpHeaders getJdyHeaders(){
        return jdyHeaders;
    }
    @Autowired
    GcDataMapper gcDataMapper;
    protected final static String testPrefix = "https://api-sandbox.xiaoman.cn";
    protected final static String prodPrefix= "https://api-sandbox.xiaoman.cn";
    protected final static String client_id = "e1033efdb7c98db1543e92bb3d3651ba67edf87ae028c7463692f6a0f13109247e3abbf446266f1a";
    protected final static String client_secret = "aeb59d76dac5a141349b3a7514c8c0ba342e871cf272ccb6107650737d6b33550c67766aa97e9922";
    protected final static String tokenUrl = prodPrefix+"/v1/oauth2/access_token";
    protected final static String customersUrl = prodPrefix+"/v1/company/list";
    protected final static String customerDetailUrl = prodPrefix+"/v1/company/info";

    protected final static String orderUrl = prodPrefix+"/v1/opportunity/list";

    protected final static String orderSubmitUrl = prodPrefix+"/v1/opportunity/push";
    protected final static String orderDetailUrl = prodPrefix+"/v1/opportunity/info";



    protected static String getHeader(String scope) throws IOException {
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("grant_type","password");
        paramMap.put("client_id",client_id);
        paramMap.put("client_secret",client_secret);
        paramMap.put("scope",scope);
        paramMap.put("username","sales16@allurecabinetry.cn");
        paramMap.put("password", EncryptSha256Util.getSha256Str("Allure2023"));
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Basic "+ Base64.getEncoder().encodeToString((client_id+":"+client_secret).getBytes()));
        String response = HttpRestUtils.post(tokenUrl, paramMap, headers);
        HashMap hashMap = JSONUtil.fromJson(response, HashMap.class);
        return (String) hashMap.get("access_token");
    };

}
